﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TangoGames.Base;
using TangoGames.Scene;
using incubation_xna3d.Scene;

namespace incubation_xna3d.Screens
{
    class Menu : Screen
    {
        IInputService input;
        SpriteBatch spriteBatch;

        Texture2D iniciar;
        Texture2D sair;
        Texture2D creditos;
        Texture2D fundo;
        Texture2D titulo;
        Texture2D tutorial;

        Texture2D ponteiro;
        Vector2 ponteiroposition;

        public Boolean chamouJogo = false;
        public Boolean chamouCreditos = false;
        public Boolean chamouTutorial = false;


        Game game;

        public Menu(Game game):base(game)
        {
            this.game = game;
            input = (IInputService)game.Services.GetService(typeof(IInputService));

            // cria o ponteiro do mouse
            ponteiro = Game.Content.Load<Texture2D>("Textures/HUD/ponteiro");
            ponteiroposition = Vector2.Zero;

            fundo = game.Content.Load<Texture2D>("Textures/Menu/fundo");
            iniciar = game.Content.Load<Texture2D>("Textures/Menu/iniciar");
            creditos = game.Content.Load<Texture2D>("Textures/Menu/creditos");
            sair = game.Content.Load<Texture2D>("Textures/Menu/sair");
            titulo = game.Content.Load<Texture2D>("Textures/Menu/titulo");
            tutorial = game.Content.Load<Texture2D>("Textures/Menu/tutorial");

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

        }

        public override void  Update(GameTime gameTime)
        {
 	         base.Update(gameTime);
             ponteiroposition.X = input.mouse.X;
             ponteiroposition.Y = input.mouse.Y;
             Botoes();

        }
        
                    

        public override void Draw(GameTime gameTime)
        { 
            spriteBatch.Begin();
            spriteBatch.Draw(fundo, new Vector2(0, 0), Color.White);
            spriteBatch.Draw(titulo, new Vector2(game.Window.ClientBounds.Width / 2 - titulo.Width / 2, game.Window.ClientBounds.Height / 2 - 300), Color.White);
            spriteBatch.Draw(iniciar, MouseOver(Localiniciar()), Color.White);
            spriteBatch.Draw(creditos, MouseOver(Localcreditos()), Color.White);
            spriteBatch.Draw(tutorial, MouseOver(Localtutorial()), Color.White);
            spriteBatch.Draw(sair, MouseOver(Localsair()), Color.White);
            spriteBatch.Draw(ponteiro, ponteiroposition, Color.White);
            spriteBatch.End();

        }

        #region Botões
        private void Botoes()
        {

            if (input.MouseClick(Localtutorial()))
            {
                
                chamouTutorial = true;
            }

            // botao iniciar
            if (input.MouseClick(Localiniciar()))
            {                
                Console.WriteLine("clicou iniciar");
                chamouJogo = true;
            }
            // botao creditos
            if (input.MouseClick(Localcreditos()))
            {
                Console.WriteLine("clicou creditos");
                chamouCreditos = true;
            }
            // botao iniciar
            if (input.MouseClick(Localsair()))
            {
                game.Exit();
            }
        }

        #endregion


        #region posição dos elementos


        public Rectangle Localtutorial()
        {
            return new Rectangle(Game.Window.ClientBounds.Width / 2 - tutorial.Width / 2, Game.Window.ClientBounds.Height / 2 + 50, tutorial.Width, tutorial.Height);
        }
        public Rectangle Localiniciar()
        {
            return new Rectangle(Game.Window.ClientBounds.Width / 2 - iniciar.Width/2, Game.Window.ClientBounds.Height / 2 - 50, iniciar.Width, iniciar.Height);
        }
        public Rectangle Localcreditos()
        {
            return new Rectangle(Game.Window.ClientBounds.Width / 2 - creditos.Width/2, Game.Window.ClientBounds.Height / 2 + 150, creditos.Width,creditos.Height);
        }
        public Rectangle Localsair()
        {
            return new Rectangle(Game.Window.ClientBounds.Width / 2 - sair.Width/2, Game.Window.ClientBounds.Height / 2 + 250,sair.Width, sair.Height);
        }


        #endregion


        #region Mouse over

        // verifica se o mouse esta sobre o botao para
        private Rectangle MouseOver(Rectangle inicial)
        {

            Rectangle destination;
            destination = new Rectangle();
            destination = inicial;

            // verifica se o mouse esta sobre o retangulo
            if (input.MouseOver(destination))
            {

                destination.X -= 2;
                destination.Y -= 2;
                destination.Width += 5;
                destination.Height += 5;


            }

            return destination;
        }
        #endregion 


    }
}
